import javax.swing.JOptionPane;


public class UpgradeTroop extends UpgradeOrder{
  
	private Territory Place;
	private int Source;
	private int Destination;
	private int Amount;
    private int[] TroopCost = {3, 11, 30, 55, 90};
    private int Cost;
    private String[] TechnologyName = {"Infantry", "Automatic Weapons", "Rocket Launchers", "Tanks", "Improved Tanks", "Fighter Planes"};
   
    public UpgradeTroop(Territory _Place, int _Source, int _Destination, int _Amount){
    	Place = _Place;
    	Source = _Source;
    	Destination = _Destination;
    	Amount = _Amount;
    	Cost = (TroopCost[Destination] - TroopCost[Source]) * Amount;
    }
    
    public boolean checkOrder(){
    	if (Source < 0) return false;
    	if (Destination <= Source) {
    		JOptionPane.showMessageDialog(null, Place.getLocation() + "You must choose a higher type!");
    		return false;
    	}
    	if (Destination > Place.getOwner().getTechnologyLevel()) {
    		JOptionPane.showMessageDialog(null, Place.getLocation() + "You can't upgrade troops beyond your maximum technology level!");
    		return false;
    	}
    	if (Amount > Place.getUsableTroopAmount(Source)) {
    		JOptionPane.showMessageDialog(null, Place.getLocation() + "No enough usable troops to upgrade! Can't upgrade a troop after move/attack!");
    		return false;
    	}
    	
    	if (Place.getOwner().getTechnology() < Cost){
    		JOptionPane.showMessageDialog(null, Place.getLocation() + "No enough technology to upgrade!");
    		return false;
    	}
    	return true;
    	}
    
    public void executeOrder(){
         Place.getOwner().cutTechnology(Cost);
         Place.upgradeTroop(Source, Destination, Amount); 	
    }

    public  String printOrder(){
    	StringBuffer sentence = new StringBuffer();
    	sentence.append("At " + Place.getLocation() + ": ");
    	sentence.append("Upgrade ");
    	sentence.append(String.valueOf(Amount) + " ");
    	sentence.append(TechnologyName[Source] + " ");
    	sentence.append("to " + TechnologyName[Destination] + " ");
    	return sentence.toString();
    }
}
